//
//  CNTopHeaderViewDelegate.m
//  BeeQuick_19
//
//  Created by clinging on 16/9/26.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNTopHeaderView.h"
#import "CZAdditions.h"
#import "Masonry.h"

@implementation CNTopHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    
    [self addButtons:@"v2_my_order_icon" title:@"我的订单" tag:kMyOrderBtn];
    [self addButtons:@"v2_my_coupon_icon" title:@"优惠券" tag:kcouponBtn];
    [self addButtons:@"v2_my_message_icon" title:@"我的消息" tag:kMyNewsBtn];
    self.backgroundColor = [UIColor whiteColor];
    
    
    /*
     kMyOrderBtn = 1,
     kcouponBtn = 2,
     kMyNewsBtn = 3,
     */
}
-(void)addButtons:(NSString *)imageName title:(NSString *)title tag:(CNTopHeaderButton)tag{
    UIButton *button = [[UIButton alloc]init];
    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:imageName] imageWH:30 title:title fontSize:12 titleColor:[UIColor darkGrayColor] spacing:5];
    [button setAttributedTitle:str forState:UIControlStateNormal];
    //自动换行
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button sizeToFit];
    
    [self addSubview:button];
    
    button.tag = tag;
    [button addTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];
}

#warning 点击
- (void)clickTopButton:(UIButton *)sender{
    
    
    [self.delegate jumpToController:sender];
}
- (void)layoutSubviews{
    //一定要调用父类的方法
    [super layoutSubviews];
    CGFloat margin = (self.frame.size.width - self.subviews[0].frame.size.width * 3)/4;
    for (NSInteger i = 0; i<self.subviews.count - 1; i++) {
        UIButton *button = self.subviews[i];
        UIButton *nextButton = self.subviews[i+1];
        
        if (i == 0) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.left.equalTo(self).offset(margin );
            }];
        }
        
        [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(button.mas_right).offset(margin);
            make.centerY.equalTo(self);
        }];
    }
}
@end
